<template>
  <div class="draw">
    <div class="container">
      <div class="statement" v-show="!showBox">
        <div class="title">签名区域(请仔细阅读下文,再签名.)</div>
        <div id="signCanvas"></div>
        <div class="btn">
          <a href="javascript:;" id="clear" ref="repeat" @click="repeat">重写</a>
          <a href="javascript:;" @click="exportImg">生成图片</a>
        </div>
        <div class="shengming">
          <h4 class="title">声明</h4>
          <div class="txt" v-if="init">
            &nbsp;&nbsp;现已收到珠海市交通运输局综合行政执法局<a class="needline" href="javascript:;">
            {{(result.receiveDate.substring(0,4))}}
          </a>
            年<a class="needline" href="javascript:;">{{(result.receiveDate.substring(6,7)) }}</a>月<a class="needline"
                                                                                                     href="javascript:;">{{(result.receiveDate.substring(9,10))}}</a>日送达的&lt&lt违法行为通知书&gt&gt<a
            class="needline" href="javascript:;">{{result.writCode}}</a>号,对&lt&lt违法行为通知书&gt&gt上的记载属于我的权力
            ,本人清楚知道并自愿放弃上述权力,承担相应的法律后果,特申请对的违法行为当即给予处罚.
            <div class="content">
              <p>声明人:<span v-html="result.name"></span></p>
              <p><a class="needline" href="javascript:;">{{(result.receiveDate.substring(0,4))}}</a> 年<a
                class="needline"
                href="javascript:;">{{(result.receiveDate.substring(6,7))
                }}</a>月<a
                class="needline" href="javascript:;">{{(result.receiveDate.substring(9,10))}}</a>日</p>
            </div>
          </div>
        </div>

      </div>
      <div class="upload" v-show="showBox">
        <div class="image-box">
          <header>
            请长按图片并保存至本地
            <input type="button" value="返回" @click="change"/>
          </header>
          <img :src="image">
          <el-upload
            class="upload-demo"
            action="http://love-li.cn/agreement/uploadSign.json"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :on-success="successuUp"
            :before-remove="beforeRemove"
            :multiple="false"
            :limit="1"
            :on-exceed="handleExceed"
            :file-list="fileList"
            :data="getid"
            @error="error"
          >
            <el-button size="small" type="primary" class="btn-success">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </div>

      </div>
    </div>
  </div>

</template>
<script type="text/ecmascript-6">
  import Draw from '../util/draw';
  import {baseurl} from "../base/config";
  import http from '../util/https'
  import {mapGetters} from "vuex"
  import newdraw from '../util/newdraw'
  export default {
    name: "draw",
    data() {
      return {
        status: 1,
        fileList: [],
        init: false,
        msg: '请在下方空白处签名',
        degree: 0,
        showBox: false,
        files: [],//上传后面数据
        show1: false,
        bookId: "",
        result: {},
        draw: "",
        image: null
      };
    },
    components: {
      Draw
    },

    beforeCreate() {
      document.title = '手写签名';
    },
    mounted() {
      this.disagreeNotice();
      this.canvasBox = document.getElementById('canvasBox');
      this.initCanvas();
      this.initfn()
      console.log(this.setuser)
    },
    computed: {
      getHorizontalStyle() {
        const d = document;
        const w = window.innerWidth || d.documentElement.clientWidth || d.body.clientWidth;
        const h = window.innerHeight || d.documentElement.clientHeight || d.body.clientHeight;
        let length = (h - w) / 2;
        let width = w;
        let height = h;
        switch (this.degree) {
          case -90:
            length = -length;
          case 90:
            width = h;
            height = w;
            break;
          default:
            length = 0;
        }
        if (this.canvasBox) {
          this.canvasBox.removeChild(document.querySelector('canvas'));
          this.canvasBox.appendChild(document.createElement('canvas'));
          setTimeout(() => {
            this.initCanvas();
          }, 200);
        }
        return {
          transform: `rotate(${this.degree}deg) translate(${length}px,${length}px)`,
          width: `${width}px`,
          height: `${height}px`,
          transformOrigin: 'center center',
        };
      },
      ...mapGetters(["setuser"]),
      getid() {
        const query = {
          illegalCaseCode: this.setuser.illegalCaseCode?this.setuser.illegalCaseCode:this.setuser.illegalCase.code
        };
        return query
      },
    },
    methods: {
      change() {
        this.showBox = !this.showBox
      },
      next() {
        this.showBox = true
      },
      repeat() {
        this.draw.clear();
      },
      exportImg() {
        this.image = this.draw.getFile();
        this.change()
      },
      initfn() {
        this.draw = new DrawCanvas()
      },
      successuUp() {
        setTimeout(() => {
          this.$router.push({
            name: "PageQuery"
          })
        }, 2000)
      },
      //获取时间
      disagreeNotice: async function () {
        const url = baseurl + "/agreement/statement/getText.json";
        let params = {
          detailListId: this.setuser.id?this.setuser.id:this.setuser.detailList.id
        };
        const res = await http.get(url, params);
        console.log(res);
        if (res.data.code === 0) {
          this.result = res.data.data;
          this.$nextTick(() => {
            this.init = true;
          })

        }
      },
      error() {
        console.log("error")
      },
      handleRemove(file, fileList) {
        console.log(file, fileList);

      },
      handlePreview(file) {
        console.log(file);
      },
      handleExceed(files, fileList) {
        // this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
      },
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${ file.name }？`);
      },
      uploadSuccess(a, b, c) {
        setTimeout(() => {
          this.show1 = false;
          this.otherParams.file = b;
          console.log(this.otherParams)
        }, 1500);
        console.log("上传完成了");
      },
      showfile() {
        this.show1 = true
      },
      inputFilter(newFile, oldFile, prevent) {
        if (newFile && !oldFile) {
          if (/(\/|^)(Thumbs\.db|desktop\.ini|\..+)$/.test(newFile.name)) {
            return prevent()
          }
          if (/\.(php5?|html?|jsx?)$/i.test(newFile.name)) {
            return prevent()
          }
        }
      },
      inputFile(newFile, oldFile) {
        if (newFile && !oldFile) {
          // add
          console.log('add', newFile)

        }
        if (newFile && oldFile) {
          // update
          console.log('update', newFile)

        }
        if (!newFile && oldFile) {
          // remove
          console.log('remove', oldFile)
        }

      },
      initCanvas() {
        const canvas = document.querySelector('canvas');
        this.draw = new Draw(canvas, -this.degree);
      },
      clear() {
        this.draw.clear();
      },
      download() {
        this.draw.downloadPNGImage(this.draw.getPNGImage());
      },
      savePNG() {
        this.signImage = this.draw.getPNGImage();
        this.showBox = true;
        alert("touch")
      },
      upload() {
        const image = this.draw.getPNGImage();
        const blob = this.draw.dataURLtoBlob(image);
        const url = '';
        const successCallback = (response) => {
          console.log(response);
        };
        const failureCallback = (error) => {
          console.log(error);
        };
        this.draw.upload(blob, url, successCallback, failureCallback);
      }
    },

  };

</script>
<style scoped lang="less" type="text/less">
  .upload-demo {
    display: inline;
  }

  .wrap {
    width: 100%;
    max-width: 750px;
    height: auto;
    min-height: 100%;
    margin: 0 auto;
    .title {
      margin-top: .5rem;
      font-size: .4rem;
      text-align: center;
      margin-bottom: .5rem;
      color: #449d44;
    }

  }

  #signCanvas {
    width: 100%;
    height: 3rem;
    background: #ccc;
    margin: 0 auto;

  }

  .btn {
    width: 100%;
    text-align: center;
    margin-top: .5rem;

  }

  .btn a {
    display: inline-block;
    width: 2rem;
    height: 0.7rem;
    line-height: 0.7rem;
    border-radius: 0.1rem;
    background: #333;
    text-align: center;
    color: #fff;
    text-decoration: none;
    font-size: .35rem;
    margin-bottom: 1rem;
  }

  #clear {
    background: #f54949;
  }

  .changeColor {
    text-align: right;
    margin-top: 0.4rem;
  }

  .changeColor a {
    margin-right: 0.3rem;
    display: inline-block;
    width: 0.2rem;
    height: 0.2rem;
    border-radius: 50%;
  }

  .changeColor a.on {
    box-shadow: 0 0 10px #4BE1FF;
  }

  .el-upload-list--text {
    display: none;
  }

  .mesTop {
    width: 100%;
    height: .8rem;
    background: #449d44;
    color: #fff;
    text-align: center;
    font-size: .35rem;
    line-height: .8rem;
  }

  .select {
    font-size: .3rem;
    width: 2rem;
    margin-bottom: -.25rem;
    background: #449d44;
    color: #fff;
    border-radius: .2rem;
    line-height: .7rem;
  }

  .btn-success {
    width: 2rem;
    text-align: center;
    border-radius: .1rem;
    vertical-align: center;
    height: .7rem;
    font-size: .35rem;
    color: #fff;
    outline: none;
    border: none;
    margin-bottom: .6rem;
  }

  .notice {
    position: fixed;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    right: 0;
    z-index: 20;
    .contentbox {
      text-align: center;
      .btn-notice {
        margin-top: 1rem;
        width: 2rem;
        height: .8rem;
        background: #449d44;
        border-radius: .3rem;
        color: #fff;
        outline: none;
        border: none;

      }
    }

    .title {
      margin-top: 1rem;
      font-size: .4rem;
      text-align: center;
      margin-bottom: .1rem;
      color: #449d44;

    }
    .space {
      margin-left: .5rem;
      .name {
        font-size: .35rem;
        margin-right: .1rem;
      }
      .FamousCustomer {
        display: inline-block;
        font-size: .35rem
      }
      .upload {
        text-align: center;
        margin-top: 1rem;
        ul {
          height: .5rem;
          width: 100%;
          margin-bottom: .3rem;
        }
        li {
          line-height: .3rem;
        }
        span {
          font-size: .3rem;

        }
      }
      .mes {
        color: red;
        font-size: .25rem;
        padding-top: 1.5rem;
        margin-right: .3rem;
        line-height: .55rem;
      }
    }
  }

  .statement {
    font-size: .35rem;
    .title {
      text-align: center;
      font-size: .3rem;
      margin: .3rem 0;
    }
    .btncont {
      text-align: center;
    }
    .btnnext {
      background: #449d44;
      height: .7rem;
      width: 1.5rem;
      line-height: .7rem;
      color: #fff;
      border-radius: .2rem;
      font-size: .3rem;
      outline: none;
      border: none;

    }
    .txt {
      font-size: .3rem;
      line-height: .5rem;
      margin-top: .3rem;
      padding: 0 .5rem;
      a {
        color: red;
      }
      .content {
        float: right;
        margin: .3rem;
        p {
          line-height: .8rem;
        }
      }
    }

  }

  header {
    margin-bottom: .3rem;
    .btn {
      background: #449d44;
      height: .7rem;
      width: 1.5rem;
      line-height: .7rem;
      color: #fff;
      border-radius: .3rem;
      font-size: .3rem;
    }
    .txt {
      font-size: .3rem;
    }
  }

  .btnClearn {
    background: #449d44;
    width: 1.8rem;
    height: .8rem;
    border-radius: .8rem;
    color: #fff;
    font-size: .3rem;
    line-height: .8rem;
  }

  .imgBtn {
    background: #c9302c;
    width: 1.8rem;
    height: .8rem;
    border-radius: .8rem;
    color: #fff;
    font-size: .3rem;
    line-height: .8rem;
  }

  .box {
    margin-top: 4rem;
  }

  .container {
    width: 100%;
    height: 100%;
    margin-bottom: 2rem;
  }

  #canvasBox {
    display: flex;
    flex-direction: column;
    height: 100%;
  }

  .greet {
    font-size: 20px;
    user-select: none;
    text-align: center;
    padding-bottom: .5rem;
    .title {
      line-height: .5rem;
    }

  }

  input {
    font-size: 20px;
  }

  .greet select {
    font-size: 18px;
  }

  canvas {
    flex: 1;
    cursor: crosshair;
    border: 2px dashed lightgray;
  }

  .image-box {
    width: 100%;
    height: 100%;
    text-align: center;
    margin-top: 1rem;
    line-height: 1rem;

  }

  .image-box header {
    font-size: 18px;
  }

  .image-box img {
    max-width: 70%;
    max-height: 70%;
    border: 1px solid gray;
  }

  .shengming {
    .title {
      margin: 0;
    }
  }

</style>
